<template>
  <div>
    <el-row type="flex" align="middle" style="padding: 5px 0px">
      <el-col :span="10">
        <div>数据源</div>
      </el-col>
      <el-col :span="4">
        <el-button
          @click="dialogVisible = true"
          type="primary"
          icon="el-icon-edit"
          circle
        ></el-button>
      </el-col>
    </el-row>

    <el-dialog
      title="数据源管理"
      destroy-on-close
      append-to-body
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      width="55%"
    >
      <el-button @click="addSource">添加数据源</el-button>
      <el-table
        :data="dataSourceList"
        style="width: 100%"
      >
        <el-table-column prop="name" label="数据源名称"> </el-table-column>
        <el-table-column prop="code" label="数据源标识"> </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editorRow(scope.$index, scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button @click.native.prevent="deleteRow(scope.row)" type="text" size="small">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        width="45%"
        title="数据源"
        destroy-on-close
        :visible.sync="innerVisible"
        append-to-body
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <el-form
          :model="ruleForm"
          label-position="top"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="数据源名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="数据源标识" prop="code">
            <el-input v-model="ruleForm.code"></el-input>
          </el-form-item>
          <el-form-item label="数据来源" prop="type">
            <el-select v-model="ruleForm.type" placeholder="请选择">
                <el-option label="自定义数据"  value="custom"> </el-option>
                <el-option label="http请求"  value="interface"> </el-option>
              </el-select>
          </el-form-item>


          <!-- 自定义 -->
          <template v-if="ruleForm.type=='custom'">
            <customData ref="customData" :form="ruleForm"></customData>
          </template>


         <!--接口获取方式  -->
          <template v-if="ruleForm.type=='interface'">
            <httpRequest  :form="ruleForm"></httpRequest>
          </template>


        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="innerVisible = false">取 消</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >保 存</el-button
          >
        </span>
      </el-dialog>

      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="setDataSource"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { Store } from "@/low-code/utils/store.js";
import clone from "clone";
import {httpRequest,customData}  from "./components/index"
export default {
  props: ["store"],
  components:{httpRequest,customData},
  data() {
    return {
      dialogVisible: false,
      innerVisible: false,
      ruleForm:{
        name:'',
        code:'',
        type:'',
        fieldType:'',
        url:'',
        method:'',
        auto:'0',
        data:null,
      },
      rules:{
       name:[
         { required: true, message: '请输入名称', trigger: 'blur' },
       ],
       code:[
         { required: true, message: '请输入标识', trigger: 'blur' },
       ]
      }
    };
  },
  mounted() {},
  methods: {
    random() {
      let len = 8;
      let $chars = "ABCDEFGHJKMNPQRSTWXYZ";
      let maxPos = $chars.length;
      let id = "";
      for (let i = 0; i < len; i++) {
        id += $chars.charAt(Math.floor(Math.random() * maxPos));
      }
      return id;
    },
    submitForm(formName) {
      let _this=this
      this.$refs[formName].validate((valid) => {
        if (valid) {

          /**自定义节点编辑器需要转换数据格式 */
          if (_this.ruleForm.fieldType == 'custom') {
            try {
               _this.ruleForm['data'] = JSON.parse(_this.$refs['customData'].eventContent) 
            } catch (error) {
            console.log("🚀 ~ file: index.vue ~ line 163 ~ this.$refs[formName].validate ~ error", error)
            }
          }


          _this.$set(_this.store.formDataSource,_this.ruleForm.code,clone(_this.ruleForm))
          this.innerVisible = false;
        } else {
          return false;
        }
      });
    },
    addSource() {
      this.innerVisible = true;
    },
    editorRow(index, row) {
      this.innerVisible = true;
      Object.assign(this.ruleForm,row)
    },
    deleteRow(row){
      this.$delete(this.store.formDataSource,row.code)
    },
    setDataSource(){
      this.store.dataSource.autoRequest()
      this.dialogVisible = false
    }
  },
  computed:{
    dataSourceList(){
      return Object.values(this.store.formDataSource)
    }
  }
};
</script>

<style lang="scss" scoped>
.form-config,
.box-model-config {
  padding-left: 10px;
}
.form-config .title,
.box-model-config .title {
  line-height: 32px;
  font-size: 14px;
}
.form-config .el-input-number,
.box-model-config .el-input-number {
  line-height: 30px;
}
.el-select{
  width: 100%;
}

::v-deep .el-form-item__label{
  padding-bottom: 5px !important;
}
::v-deep .el-form-item{
    margin-bottom: 5px;
}
</style>